package com.xz.group12306.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xz.group12306.business.common.TextUtils;
import com.xz.group12306.business.service.ManagerService;
import com.xz.group12306.business.service.impl.ManagerServiceImpl;
import com.xz.group12306.domain.Ticket;

/**
 * 增加车票信息的Servlet
 */
@WebServlet("/ManageAddTicket")
public class ManageAddTicketServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * 构造方法
	 */
	public ManageAddTicketServlet() {
		super();
	}

	/**
	 * doGet方法
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * doPost方法
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		String depature = request.getParameter("depature");
		String destinction = request.getParameter("destinction");
		String trainno = request.getParameter("trainno");
		String starttime = request.getParameter("starttime");
		Integer seatno = Integer.parseInt(request.getParameter("seatno"));
		String costtime = request.getParameter("costtime");
		int ticketno =(Integer.parseInt(request.getParameter("ticketno")));
		
		Ticket ticket = new Ticket();
		ticket.setDepature(depature);
		ticket.setDestinction(destinction);
		ticket.setTrainNo(trainno);
		ticket.setTicketNo(ticketno);
		
		if(!TextUtils.isEmpty(starttime)) {
			ticket.setStartTime(Date.valueOf(starttime));
		}
		ticket.setSeatNo(seatno);
		ticket.setCostTime(costtime);

		ManagerService managerService=new ManagerServiceImpl();
		managerService.addTicket(ticket);
		out.print("插入成功");

		List<Ticket> tickets = managerService.findAllTicket();
		HttpSession session = request.getSession();

		session.setAttribute("tickets", tickets);
		response.sendRedirect("admin/UpdateTicket.jsp");
	}

}
